package com.junwell.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.junwell.common.dto.PageDto;
import com.junwell.common.lang.PageResult;
import com.junwell.common.lang.Result;
import com.junwell.entity.Manager;
import com.junwell.service.ManagerService;
import com.junwell.service.impl.ManagerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    ManagerService managerService;

    @GetMapping("/admin/findPage")
    public IPage<Manager> findPage(PageDto pageDto){
        IPage<Manager> page = new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("login_name"  , pageDto.getQueryString());
        page  = managerService.page(page, queryWrapper);
        return page;
    }

    @PostMapping("/admin/add")
    public Result add(@RequestBody Manager manager){
        boolean add = managerService.add(manager);
        if (add){
            return Result.succ("成功");
        }else {
            return Result.fail("添加失败");
        }
    }



    @GetMapping("/admin/{id}")
    public Result viewOne(@PathVariable("id")Integer id){
        Manager manager = managerService.viewOne(id);
        if (manager !=null){
            return Result.succ(200,"成功",manager);
        }else {
            return Result.fail("失败");
        }
    }


    @PostMapping("/admin/update")
    public Result update(@RequestBody Manager manager){
        boolean update = managerService.update(manager);
        if (update){
            return Result.succ("成功");
        }else {
            return Result.fail("失败");
        }
    }

    @DeleteMapping("/admin/delete/{id}")
    public Result delete(@PathVariable Integer id ){
        boolean delete = managerService.delete(id);
        if (delete){
            return Result.succ("成功");
        }else {
            return Result.fail("失败");
        }
    }



}
